package da.metricscraft.listeners;
import java.util.HashMap;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockPlaceEvent;

import da.metricscraft.MetricsCraft;
import da.metricscraft.information.PlayerInformation.PlayerDataType;


public class MetricsBlockListener implements Listener{
	public static MetricsCraft plugin;
	private HashMap<Material, Integer> fineMats;
	private HashMap<Material, Integer> normalMats;
	private HashMap<Material, Integer> buildMats;
	private HashMap<Material, Integer> redstoneMats;
	private HashMap<Material, String> perfectItemFits;
	private HashMap<String, Integer> itemFitValues;
	
	public MetricsBlockListener(MetricsCraft instance) {
		plugin = instance;
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
		
		redstoneMats = new HashMap<Material, Integer>();
		redstoneMats.put(Material.REDSTONE, 1);
		redstoneMats.put(Material.REDSTONE_TORCH_OFF, 2);
		redstoneMats.put(Material.REDSTONE_TORCH_ON, 2);
		redstoneMats.put(Material.REDSTONE_WIRE, 1);
		redstoneMats.put(Material.PISTON_BASE, 4);
		redstoneMats.put(Material.PISTON_STICKY_BASE, 8);
		redstoneMats.put(Material.POWERED_MINECART, 6);
		redstoneMats.put(Material.POWERED_RAIL, 6);
		redstoneMats.put(Material.RAILS, 3);
		redstoneMats.put(Material.DETECTOR_RAIL, 5);
		redstoneMats.put(Material.MINECART, 3);
		redstoneMats.put(Material.TNT, 6);
		redstoneMats.put(Material.TRAP_DOOR, 2);
		redstoneMats.put(Material.WOOD_DOOR, 2);
		redstoneMats.put(Material.IRON_DOOR, 4);
		redstoneMats.put(Material.LEVER, 2);
		redstoneMats.put(Material.WOOD_PLATE, 2);
		redstoneMats.put(Material.STONE_BUTTON, 2);
		redstoneMats.put(Material.STONE_PLATE, 2);
		
		itemFitValues = new HashMap<String, Integer>();
		itemFitValues.put("WOOD", 1);
		itemFitValues.put("STONE", 2);
		itemFitValues.put("IRON", 3);
		itemFitValues.put("GOLD", 4);
		itemFitValues.put("DIAMOND", 5);
		
		
		perfectItemFits = new HashMap<Material, String>();
		perfectItemFits.put(Material.COBBLESTONE, "PICKAXE");
		perfectItemFits.put(Material.STONE, "PICKAXE");
		perfectItemFits.put(Material.SANDSTONE, "PICKAXE");
		perfectItemFits.put(Material.COAL_ORE, "PICKAXE");
		perfectItemFits.put(Material.DIAMOND_ORE, "PICKAXE");
		perfectItemFits.put(Material.REDSTONE_ORE, "PICKAXE");
		perfectItemFits.put(Material.IRON_ORE, "PICKAXE");
		perfectItemFits.put(Material.LAPIS_ORE, "PICKAXE");
		perfectItemFits.put(Material.GOLD_ORE, "PICKAXE");
		perfectItemFits.put(Material.MOSSY_COBBLESTONE, "PICKAXE");
		perfectItemFits.put(Material.NETHERRACK, "PICKAXE");
		perfectItemFits.put(Material.OBSIDIAN, "PICKAXE");
		
		perfectItemFits.put(Material.SAND, "SHOVEL");
		perfectItemFits.put(Material.DIRT, "SHOVEL");
		perfectItemFits.put(Material.GRAVEL, "SHOVEL");
		perfectItemFits.put(Material.CLAY, "SHOVEL");
		perfectItemFits.put(Material.GRASS, "SHOVEL");
		perfectItemFits.put(Material.SOUL_SAND, "SHOVEL");
		
		perfectItemFits.put(Material.WOOD, "AXE");
		perfectItemFits.put(Material.LOG, "AXE");

		
		normalMats = new HashMap<Material, Integer>();
		normalMats.put(Material.COBBLESTONE, 1);
		normalMats.put(Material.STONE, 1);
		normalMats.put(Material.SAND, 1);
		normalMats.put(Material.SANDSTONE, 2);
		normalMats.put(Material.WOOD, 1);
		normalMats.put(Material.LOG, 1);
		normalMats.put(Material.DIRT, 1);
		normalMats.put(Material.GRAVEL, 1);
		normalMats.put(Material.GRASS, 1);
		normalMats.put(Material.COAL_ORE, 2);
		normalMats.put(Material.CLAY, 3);
		
		fineMats = new HashMap<Material, Integer>();
		fineMats.put(Material.DIAMOND_ORE, 20);
		fineMats.put(Material.REDSTONE_ORE, 12);
		fineMats.put(Material.IRON_ORE, 3);
		fineMats.put(Material.LAPIS_ORE, 14);
		fineMats.put(Material.GOLD_ORE, 14);
		fineMats.put(Material.GLOWSTONE, 10);
		fineMats.put(Material.SOUL_SAND, 12);
		fineMats.put(Material.MOSSY_COBBLESTONE, 12);
		fineMats.put(Material.NETHERRACK, 8);
		fineMats.put(Material.OBSIDIAN, 18);
		fineMats.put(Material.RAILS, 6);
		fineMats.put(Material.WEB, 8);
		
		buildMats = new HashMap<Material, Integer>();
		buildMats.put(Material.COBBLESTONE, 1);
		buildMats.put(Material.COBBLESTONE_STAIRS, 3);
		buildMats.put(Material.DIRT, 1);
		buildMats.put(Material.SAND, 1);
		buildMats.put(Material.SANDSTONE, 3);
		buildMats.put(Material.GRAVEL, 1);
		buildMats.put(Material.LADDER, 2);
		buildMats.put(Material.WOOD, 1);
		buildMats.put(Material.WOOD_STAIRS, 3);
		buildMats.put(Material.WOOD_PLATE, 2);
		buildMats.put(Material.LOG, 2);
		buildMats.put(Material.STONE, 4);
		buildMats.put(Material.STONE_PLATE, 8);
		buildMats.put(Material.GLASS, 4);
		buildMats.put(Material.BRICK, 4);
		buildMats.put(Material.BRICK_STAIRS, 6);
		buildMats.put(Material.WOOL, 4);
		buildMats.put(Material.CLAY, 4);
		buildMats.put(Material.IRON_BLOCK, 16);
		buildMats.put(Material.LAPIS_BLOCK, 22);
		buildMats.put(Material.OBSIDIAN, 22);
		buildMats.put(Material.GOLD_BLOCK, 24);
		buildMats.put(Material.DIAMOND_BLOCK, 48);
	}
	@EventHandler
	public void onBlockDamage(BlockDamageEvent event) {

		Block block = event.getBlock();
		Player player = event.getPlayer();
	};
	@EventHandler
	public void onBlockBreak(BlockBreakEvent event){
		
		Block block = event.getBlock();
		Player player = event.getPlayer();
		short currentDur = player.getItemInHand().getDurability();
		short maxDur = Material.getMaterial(player.getItemInHand().getTypeId()).getMaxDurability();
		String item = Material.getMaterial(player.getItemInHand().getTypeId()).name();
		if (plugin.enabled(player)){
			if (fineMats.containsKey(block.getType())){
				plugin.addPlayerData(player, PlayerDataType.FINE_MINED, fineMats.get(block.getType()));
			}else if (normalMats.containsKey(block.getType())){
				plugin.addPlayerData(player, PlayerDataType.MINED, 1);
			}else if (redstoneMats.containsKey(block.getType())){
				plugin.addPlayerData(player, PlayerDataType.DEVELOP_REMOVE, redstoneMats.get(block.getType()));
			}
			
			if (perfectItemFits.containsKey(block.getType())){
				if (item.contains(perfectItemFits.get(block.getType()))){
					int indexOfUnderscore = item.indexOf("_");
					String s = item.substring(0, indexOfUnderscore);
					plugin.addPlayerData(player, PlayerDataType.FITTING_ITEMS_USED, itemFitValues.get(s));
				}
			}
			
			if (currentDur == maxDur) MetricsCraft.log.info("Destroyed " + player.getDisplayName() + "'s " + item);
		}
	}
	@EventHandler
	public void onBlockPlace(BlockPlaceEvent event) {

		Block block = event.getBlock();
		Player player = event.getPlayer();
		if (plugin.enabled(player)){
			if (buildMats.containsKey(block.getType())){
				plugin.addPlayerData(player, PlayerDataType.BUILT, buildMats.get(block.getType()));
			}else if (redstoneMats.containsKey(block.getType())){
				plugin.addPlayerData(player, PlayerDataType.DEVELOP, redstoneMats.get(block.getType()));
			}
//			player.sendMessage("You just placed a " + block.getType().name() + "-Block " +
//					"at " + block.getLocation().getBlockX() + "/" + block.getLocation().getBlockY() + "/" + block.getLocation().getBlockZ() + "!");
		}
	}
}
